package com.example.gzysmartcity.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.model.SCNewsComment;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class NewsXpViewModel extends ViewModel {
    MutableLiveData<State> newsXpLiveData=new MutableLiveData<>();
    List<SCNewsComment.DataBean> mnewscDataBeanList;

    public void getNewsCommentData(){
        ThreadUtils.executeByCached(new ThreadUtils.Task<SCNewsComment>() {
            @Override
            public SCNewsComment doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("SCNewsComment/getAll?", null, SCNewsComment.class, 0);
            }

            @Override
            public void onSuccess(SCNewsComment result) {
                mnewscDataBeanList=result.getData();
                newsXpLiveData.postValue(State.SUCCESS);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
                newsXpLiveData.postValue(State.FAIL);
            }
        });
    }

    public List<SCNewsComment.DataBean> getNewIdList(int id){
        List<SCNewsComment.DataBean> dataBeans=new ArrayList<>();
        dataBeans.addAll(mnewscDataBeanList.stream().filter(a->a.getSCNewsId()==id).collect(Collectors.toList()));
        return dataBeans;
    }

    public MutableLiveData<State> getNewsXpLiveData() {
        return newsXpLiveData;
    }

    public List<SCNewsComment.DataBean> getMnewscDataBeanList() {
        return mnewscDataBeanList;
    }
}
